<?php
require_once '../common/RootPath.php';
RootPath::importPath("dao/BaseMapper.php");
RootPath::importPath("config/Database.php");
RootPath::importPath("entity/Article.php");
RootPath::importPath("entity/Tag.php");
RootPath::importPath("entity/SysUser.php");

class ArticleMapper implements BaseMapper
{

    private $db;

    public function __construct()
    {
        $this->db = new Database();
    }

    public function insert($record)
    {
        // TODO: Implement insert() method.
    }

    public function selectByPrimaryKey($id)
    {
        // TODO: Implement selectByPrimaryKey() method.
    }

    public function selectAllPage($pagination)
    {
        $offset = $pagination->getOffset();
        $pageSize = $pagination->getPageSize();
        $page = $pagination->getPage();

        $sql="
            SELECT
            a.id,
            a.comment_counts,
            a.create_date,
            a.summary,
            a.title,
            a.view_counts,
            a.weight,
            a.author_id,
            a.body_id,
            a.category_id,
            t.id tag_id,
            t.tag_name,
            t.avatar,
            u.id user_id,
            u.account,
            u.admin,
            u.avatar user_avatar,
            u.create_date user_createDate,
            u.deleted,
            u.email,
            u.last_login,
            u.mobile_phone_number,
            u.nickname,
            u.password,
            u.salt,
            u.status
        FROM
            ms_article a
            JOIN ms_article_tag at1 ON a.id = at1.article_id
            JOIN ms_tag t ON at1.tag_id = t.id
            JOIN ms_sys_user u ON a.author_id = u.id
            ORDER BY a.weight,a.create_date DESC LIMIT $offset,$pageSize
        ";
        return $sql;

      /*  $records = $this->db->customSQLWithPage($sql);

        $count = JsonResult::getValue($records,"total");
        $arrRecords = JsonResult::json2analysis($records);

        $articles = [];
        $tags = [];
        $users = [];
        foreach ($arrRecords as $row) {
            $articleId = $row['id'];
            if (!isset($articles[$articleId])) {
                $articles[$articleId] = new Article($articleId, $row['title'], $row['summary'], $row['comment_counts'], $row['view_counts'], $row['weight'],$row['create_date'],$row['author_id']);
            }

            // 创建 Tag 实例并存入临时 $tags 数组
            $tags[] = new Tag($row['tag_id'], $row['tag_name'], $row['avatar']);

            // 将 tags 赋值给对应的 Article
            $articles[$articleId]->setTags($tags);

            $users[] =new SysUser(
                $row['user_id'],
                $row['account'],
                $row['admin'],
                $row['user_avatar'],
                $row['user_createDate'],
                $row['deleted'],
                $row['email'],
                $row['last_login'],
                $row['mobile_phone_number'],
                $row['nickname'],
                $row['password'],
                $row['salt'],
                $row['status']
            );

            $articles[$articleId]->setAuthor($users);

            // 清空 tags 数组，为下一个 Article 重置
            $tags = [];
            $users = [];
        }

          return JsonResult::customDaoQuery2Json($count,$articles);*/

    }

    public function updateByPrimaryKey($id)
    {
        // TODO: Implement updateByPrimaryKey() method.
    }

    public function deleteByPrimaryKey($id)
    {
        // TODO: Implement deleteByPrimaryKey() method.
    }

    public function selectByKeywordPaged($queryDto,$pagination)
    {
        $offset = $pagination->getOffset();
        $pageSize = $pagination->getPageSize();
        $page = $pagination->getPage();

        $keyword = $queryDto->keyword;

        $sql="
            select * from ms_article WHERE title LIKE '%{$keyword}%' ORDER BY weight,create_date DESC LIMIT $offset,$pageSize
        ";

        echo $this->db->customSQLWithPage($sql);

    }


}